package com.dingreading.cloud.mall.controller;

import com.alibaba.fastjson.JSONObject;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.wechat.AuthToken;
import com.dingreading.cloud.common.util.wechat.WxPayBusiness;
import com.dingreading.cloud.mall.entity.OrderForm;
import com.dingreading.cloud.mall.service.OrderFormService;
import com.dingreading.cloud.mall.util.redis.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/front/wechat")
@Api(tags = "商城-微信开发管理")
public class FrontWxGzhController {

    // 叮叮文化
    @Value("${wx.appId}")
    private String appId;
    @Value("${wx.appSecret}")
    private String appSecret;
    // 叮叮书房文化
    @Value("${wx.sf.appId}")
    private String shAppId;
    @Value("${wx.sf.appSecret}")
    private String shAppSecret;

    @Resource
    private OrderFormService orderFormService;
    @Resource
    protected RedisService redisService;

    @ApiOperation(value = "通过商城订单号获取微信公众号的appid")
    @PostMapping(value = "/getAppIdByOrderCode", produces = "application/json; charset=UTF-8")
    public R<Object> getAppIdByOrderCode(
            @RequestParam(value = "orderCode") @ApiParam(value = "商城订单号") String orderCode
    ) throws Exception {
        OrderForm orderForm = orderFormService.getByCode(null, orderCode);
        if (orderForm == null)
            return R.fail("订单获取失败");
        if (orderForm.getPayCorp() == null)
            return R.fail("订单的收款公司获取失败");

        if (orderForm.getPayCorp().equals(1)) {
            // 叮叮文化
            return R.ok(appId);
        } else {
            // 叮叮书房
            return R.ok(shAppId);
        }
    }

    @ApiOperation(value = "通过appId获取微信openid接口")
    @PostMapping(value = "/getOpenIdByAppId", produces = "application/json; charset=UTF-8")
    public R<Object> getOpenIdByAppId(
            @RequestParam(value = "appId") @ApiParam(value = "微信公众号的appid") String appId,
            @RequestParam(value = "code") @ApiParam(value = "微信授权码") String code
    ) throws Exception {
        String redisKey = "authAccess_" + code;
        String redisAccessToken = (String) redisService.get(redisKey);

        AuthToken authToken = null;
        if (StringUtils.isNotEmpty(redisAccessToken)) {
            authToken = JSONObject.parseObject(redisAccessToken, AuthToken.class);
        } else {
            if (appId.equals(this.appId)) {
                // 叮叮文化
                authToken = WxPayBusiness.Init().getAccessToken(code, this.appId, appSecret);
            } else {
                // 叮叮书房
                authToken = WxPayBusiness.Init().getAccessToken(code, shAppId, shAppSecret);
            }

            // 缓存code获取的accessToken
            redisService.set(redisKey, JSONObject.toJSONString(authToken), 1800);
        }

        if (authToken != null) {
            if (authToken.getErrcode() == null)
                return R.ok(authToken.getOpenid());
            else
                return R.fail(authToken.getErrmsg());
        }
        return R.fail("获取微信openid失败");
    }

}
